package presentacion;

import integracion.DatabaseManager;
import integracion.TareaDAO;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import share.Tarea;

public class ModificarTareas extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{

	   static final long serialVersionUID = 1L;


		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
			doAll(request, response);
		}

		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
			doAll(request, response);
		}

		private void doAll(HttpServletRequest request, HttpServletResponse response) throws IOException
		{

			if(request.getParameter("accion").equals("Modificar"))
			{

				Tarea t = new Tarea();
				t.setCodigo(Integer.parseInt(request.getParameter("id")));
				t.setNombre(request.getParameter("nombre"));
				t.setBitacora(request.getParameter("bitacora"));
				t.setFecha_Inicio(request.getParameter("Finicial"));
				t.setFecha_Final(request.getParameter("FFinal"));

				try
				{
					TareaDAO dao = new TareaDAO(DatabaseManager.getConnection());
					dao.modificar(t);
					response.sendRedirect("index.jsp?page=GestionarTareas/GestionarTareas");
					return;

				} catch (SQLException e)
				{
					e.printStackTrace();
				}
			}

			if(request.getParameter("accion").equals("datos"))
			{
				int id = Integer.parseInt(request.getParameter("opciones"));
				Tarea t = new Tarea();

				try
				{
					TareaDAO dao = new TareaDAO(DatabaseManager.getConnection());
					t= dao.getTareaById(id);
					System.out.println("index.jsp?page=GestionarTareas/formularioModificarTareas&id="+t.getCodigo()+
					"&nombre="+t.getNombre()+"&duracion="+t.getDuracion()+"&bitacora="+t.getBitacora()+
					"&Finicial="+t.getFecha_Inicio()+"&FFinal="+t.getFecha_Final());

					response.sendRedirect("index.jsp?page=GestionarTareas/formularioModificarTareas&id="+t.getCodigo()+
					"&nombre="+t.getNombre()+"&duracion="+t.getDuracion()+"&bitacora="+t.getBitacora()+
					"&Finicial="+t.getFecha_Inicio()+"&FFinal="+t.getFecha_Final());
				}
				catch (SQLException e)
				{
					e.printStackTrace();
				}
			}


		}
}
